﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DatabaseToCode.Schema
{
    public class Table
    {
        public Table() {
            this.Columns = new Dictionary<string, Column>();
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>
        /// The name.
        /// </value>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the display name.
        /// </summary>
        /// <value>
        /// The display name.
        /// </value>
        public string DisplayName { get; set; }

        /// <summary>
        /// Gets the name of the class.
        /// </summary>
        /// <value>
        /// The name of the class.
        /// </value>
        public string ClassName { get { return this.DisplayName.Replace(" ", string.Empty); } }

        /// <summary>
        /// Gets the columns.
        /// </summary>
        public Dictionary<string, Column> Columns { get; private set; }

        /// <summary>
        /// Gets or sets the name of the primary key.
        /// </summary>
        /// <value>
        /// The name of the primary key.
        /// </value>
        public string PrimaryKeyName { get; set; }

        //HACK: en attendant de trouver mieux ;)
        /// <summary>
        /// Gets the primary key for view.
        /// </summary>
        /// <returns></returns>
        public string GetPrimaryKeyForView() {
            if (!this.PrimaryKeyName.Contains(',')) {
                return this.PrimaryKeyName;
            }

            return this.PrimaryKeyName.Split(',')[0];
        }

        /// <summary>
        /// Gets the primary key for db context.
        /// </summary>
        /// <returns></returns>
        public string GetPrimaryKeyForDbContext() {
            var keys = new List<string>();
            this.Columns.Where(c => c.Value.IsKey).ToList().ForEach(c=>keys.Add("t." + c.Value.FieldName));

            return string.Join<string>(", ", keys);
        }
    }
}
